<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card
        v-loading="loading"
        element-loading-text="拼命加载中..."
        class="tree-card"
      >
        <!-- 树形组件 -->
        <TreeTools root :node-data="company" :is-root="true" @add="addFn" />
        <el-tree :data="list" :props="defaultProps" default-expand-all>
          <template v-slot="{data}">
            <TreeTools
              :node-data="data"
              @getDeparts="getDeparts"
              @add="addFn"
              @edit="editFn"
            />
          </template>
        </el-tree>
      </el-card>
      <AddDept
        ref="add"
        :show-dialog="showDialog"
        :node-data="nodeData"
        @close="close"
        @getDeparts="getDeparts"
      />
      <!-- @getDepartments="getDepartments" -->
    </div>
  </div>
</template>

<script>
import AddDept from './components/AddDept.vue'
import { listToTree } from '@/utils/index'
import { getDepartementList } from '@/api/departements'
import TreeTools from './components/TreeTools.vue'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      loading: false,
      showDialog: false,
      nodeData: {}
    }
  },
  created() {
    this.getDeparts()
  },
  methods: {
    async getDeparts() {
      // this.loading = true
      const res = await getDepartementList()
      // console.log(res)
      // 保存部门数据
      this.list = listToTree(res.depts)
      // 保存公司名字
      // console.log(res.companyName)
      this.company.name = res.companyName
      this.loading = false
    },
    addFn(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
    },
    close() {
      this.showDialog = false
    },
    editFn(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // 数据回显
      // this.$refs.add.form = { ...nodeData }
      this.$refs.add.getInfo(nodeData.id)
    }
  }
}

</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 140px;
  font-size:14px;
  .box {
    width: 80px;
  }
  ::v-deep {
    .el-col {
      height: 30px;
      line-height: 30px;
    }
    .el-icon-caret-right:before {
      content: "\e723";
      color: #000;
    }
    .el-tree-node__expand-icon.expanded {
      transform: rotate(0deg);
    }
    .el-tree-node__expand-icon.expanded::before {
      content: "\e722";
      color: #000;
    }

    .el-tree-node__expand-icon.is-leaf::before {
      content: "\e7a5";
      color: #000;
    }
  }
}
</style>
